package com.castle.fortress.admin.cms.fragment;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.cms.dto.CmsPageDto;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import org.beetl.core.tag.GeneralVarTagBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cms 分页标签
 * @author castle
 */
public class CmsPageTag extends GeneralVarTagBinding {

	@Override
	public void render() {
		CmsPageDto pageinfo = new CmsPageDto();
		if(containHtmlAttribute("totalpage")&&containHtmlAttribute("currenturl")
				&&StrUtil.isNotEmpty(getAttributeValue("totalpage").toString())
				&&StrUtil.isNotEmpty(getAttributeValue("currenturl").toString())){
			try {
				Integer showpage = 3;
				if(containHtmlAttribute("showpage")&&StrUtil.isNotEmpty(getAttributeValue("showpage").toString())){
					try {
						Integer i = Integer.parseInt(getHtmlAttribute("showpage").toString());
						if(i>0){
							showpage = i;
						}
					} catch (NumberFormatException e) {
					}
				}
				Integer totalpage = null;
				try {
					totalpage = Integer.valueOf(getHtmlAttribute("totalpage").toString());
					//数据错误 不渲染
					if(totalpage<1 ){
						this.binds(pageinfo);
						this.doBodyRender();
						return;
					}
				} catch (NumberFormatException e) {
					this.binds(pageinfo);
					this.doBodyRender();
					return;
				}
				String currenturl = getHtmlAttribute("currenturl").toString();
				if(StrUtil.isEmpty(currenturl)){
					this.binds(pageinfo);
					this.doBodyRender();
					return;
				}
				Integer current = 0;
				//url路径处理
				String urlPre="",urlEnd="",code="";
				boolean searchFlag = false;
				if(currenturl.indexOf("/page/search")!=-1){
					searchFlag = true;
				}
				//搜索页
				if(searchFlag){
					String name="";
					Integer pagesize= GlobalConstants.DEFAULT_PAGE_SIZE,pageindex=GlobalConstants.DEFAULT_PAGE_INDEX;
					Integer urlIndex = currenturl.lastIndexOf("?");
					if(urlIndex<0){
						this.binds(pageinfo);
						this.doBodyRender();
						return;
					}
					String urlInfo = currenturl.substring(urlIndex+1);
					String[] params = urlInfo.split("&");
					for(String s:params){
						String[] ss = s.split("=");
						if(ss.length == 2){
							if("name".equals(ss[0])){
								name = ss[1];
							}else if("pagesize".equals(ss[0])){
								try {
									Integer i = Integer.parseInt(ss[1]);
									if(i>0){
										pagesize =i;
									}
								} catch (NumberFormatException e) {

								}
							}else if("pageindex".equals(ss[0])){
								try {
									Integer i = Integer.parseInt(ss[1]);
									if(i>0){
										pageindex =i;
									}
								} catch (NumberFormatException e) {

								}
							}
						}
					}
					if(StrUtil.isEmpty(name)){
						this.binds(pageinfo);
						this.doBodyRender();
						return;
					}
					urlPre="/page/search?name="+name+"&pagesize="+pagesize+"&pageindex=";
					current = pageindex;
				//普通列表页
				}else{
					Integer urlIndex = currenturl.lastIndexOf("/");
					if(urlIndex>0 && urlIndex<currenturl.length()){
						urlPre = currenturl.substring(0,urlIndex+1);
						urlEnd = currenturl.substring(urlIndex+1);
						String[] s = urlEnd.split("-");
						if(s.length == 1){
							code = s[0];
							current=1;
						}else if(s.length== 2){
							code = s[0];
							current = Integer.parseInt(s[1]);
							if(current<1 || current>totalpage){
								this.binds(pageinfo);
								this.doBodyRender();
								return;
							}
							//数据错误 不渲染
						}else{
							this.binds(pageinfo);
							this.doBodyRender();
							return;
						}
						//数据错误 不渲染
					}else{
						this.binds(pageinfo);
						this.doBodyRender();
						return;
					}
				}

				//上一页判断
				if(current == 1){
					pageinfo.setHaspre(false);
				}else{
					pageinfo.setHaspre(true);
					if(searchFlag){
						pageinfo.setPre(urlPre+(current-1));
					}else{
						pageinfo.setPre(urlPre+code+(current==2?"":("-"+(current-1))));
					}
				}
				//下一页判断
				if(current.equals(totalpage)){
					pageinfo.setHasnext(false);
				}else{
					pageinfo.setHasnext(true);
					if(searchFlag){
						pageinfo.setPre(urlPre+(current+1));
					}else{
						pageinfo.setNext(urlPre+code+("-"+(current+1)));
					}
				}
				//全部页码
				List<Map<String,Object>> list=new ArrayList<>();
				//当前页之前的页码
				List<Map<String,Object>> preList=new ArrayList<>();
				Integer presize=0;
				//超过7个则省略号显示
				if(totalpage>7 && showpage<(totalpage-2)){
					int prenum = showpage/2;
					//偶数块 前多取一位
					if(showpage%2==0){
						prenum = (showpage+1)/2;
					}
					if(current>prenum&&(current-prenum)>2){
						Map<String,Object> m1 = new HashMap<>();
						if(searchFlag){
							m1.put("url",urlPre+1);
						}else{
							m1.put("url",urlPre+code);
						}
						m1.put("index",1);
						preList.add(m1);
						list.add(m1);
						Map<String,Object> mm = new HashMap<>();
						mm.put("url","");
						mm.put("index","...");
						preList.add(mm);
						list.add(mm);
						for(int i =(current-prenum);(i>2&&i<current);i++){
							Map<String,Object> m = new HashMap<>();
							if(searchFlag){
								m.put("url",urlPre+i);
							}else{
								m.put("url",urlPre+code+(i>1?("-"+(i)):""));
							}
							m.put("index",i);
							preList.add(m);
							list.add(m);
						}
					}else{
						for(int i=1;i<current;i++){
							Map<String,Object> m = new HashMap<>();
							if(searchFlag){
								m.put("url",urlPre+i);
							}else{
								m.put("url",urlPre+code+(i>1?("-"+(i)):""));
							}
							m.put("index",i);
							preList.add(m);
							list.add(m);
						}
					}
				//小于7个全显示
				}else{
					for(int i=1;i<current;i++){
						Map<String,Object> m = new HashMap<>();
						if(searchFlag){
							m.put("url",urlPre+i);
						}else{
							m.put("url",urlPre+code+(i>1?("-"+(i)):""));
						}
						m.put("index",i);
						preList.add(m);
						list.add(m);
					}
				}

				presize=preList.size();
				pageinfo.setPrelist(preList);
				pageinfo.setPresize(presize);

				//当前页
				pageinfo.setCurrent(current);
				pageinfo.setCurrenturl(currenturl);
				Map<String,Object> c = new HashMap<>();
				c.put("url",currenturl);
				c.put("index",current);
				list.add(c);

				//后续页的页码
				List<Map<String,Object>> nextList=new ArrayList<>();
				Integer nextsize=0;
				if(totalpage>7 && showpage<(totalpage-2)){
					int prenum = showpage/2;
					//偶数块 后少取一位
					if(showpage%2==0){
						prenum = (showpage-1)/2;
					}
					if((current+prenum)<=(totalpage-2)){
						for(int i =(current+1);i<=(current+prenum);i++){
							Map<String,Object> m = new HashMap<>();
							if(searchFlag){
								m.put("url",urlPre+i);
							}else{
								m.put("url",urlPre+code+("-"+i));
							}
							m.put("index",i);
							nextList.add(m);
							list.add(m);
						}
						Map<String,Object> mm = new HashMap<>();
						mm.put("url","");
						mm.put("index","...");
						nextList.add(mm);
						list.add(mm);
						Map<String,Object> m1 = new HashMap<>();
						if(searchFlag){
							m1.put("url",urlPre+totalpage);
						}else{
							m1.put("url",urlPre+code+(totalpage>1?("-"+(totalpage)):""));
						}
						m1.put("index",totalpage);
						nextList.add(m1);
						list.add(m1);
					}else{
						for(int i=(current+1);i<=totalpage;i++){
							Map<String,Object> m = new HashMap<>();
							if(searchFlag){
								m.put("url",urlPre+i);
							}else{
								m.put("url",urlPre+code+("-"+i));
							}
							m.put("index",i);
							nextList.add(m);
							list.add(m);
						}
					}
				}else{
					for(int i=(current+1);i<=totalpage;i++){
						Map<String,Object> m = new HashMap<>();
						if(searchFlag){
							m.put("url",urlPre+i);
						}else{
							m.put("url",urlPre+code+("-"+i));
						}
						m.put("index",i);
						nextList.add(m);
						list.add(m);
					}
				}
				nextsize=nextList.size();
				pageinfo.setNextlist(nextList);
				pageinfo.setNextsize(nextsize);
				pageinfo.setTotalpage(totalpage);
				if(searchFlag){
					pageinfo.setFirst(urlPre+1);
					pageinfo.setEnd(urlPre+totalpage);
				}else{
					pageinfo.setFirst(urlPre+code);
					pageinfo.setEnd(urlPre+code+(totalpage>1?("-"+(totalpage)):""));
				}
				pageinfo.setList(list);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		this.binds(pageinfo);
		this.doBodyRender();
	}
}
